package util

import (
	"bytes"
	"fmt"
	"strings"
)

// 换行
const NL string = "\n"

// 缩进
const TAB string = "    "

const NT string = "\n" + TAB
const NTT string = "\n" + TAB + TAB
const NTTT string = "\n" + TAB + TAB + TAB
const NTTTT string = "\n" + TAB + TAB + TAB + TAB
const NTTTTT string = "\n" + TAB + TAB + TAB + TAB + TAB
const NTTTTTT string = "\n" + TAB + TAB + TAB + TAB + TAB + TAB
const NTTTTTTT string = "\n" + TAB + TAB + TAB + TAB + TAB + TAB + TAB
const NTTTTTTTT string = "\n" + TAB + TAB + TAB + TAB + TAB + TAB + TAB + TAB

type CodeBuffer struct {
	value *bytes.Buffer
}

func NewCodeBuffer() *CodeBuffer {
	buffer := &CodeBuffer{
		value: bytes.NewBufferString(""),
	}
	return buffer
}

func (b *CodeBuffer) Append(str string) *CodeBuffer {
	b.value.WriteString(str)
	return b
}

func (b *CodeBuffer) Appendf(format string, args ...interface{}) *CodeBuffer {
	b.value.WriteString(fmt.Sprintf(format, args...))
	return b
}

func (b *CodeBuffer) ToString() string {
	return fmt.Sprintf("%v", b.value)
}

func (b *CodeBuffer) Format(format string, args ...interface{}) *CodeBuffer {
	b.value.WriteString(fmt.Sprintf(format, args...))
	return b
}

func (b *CodeBuffer) Nformat(format string, args ...interface{}) *CodeBuffer {
	b.value.WriteString(NL)
	b.value.WriteString(fmt.Sprintf(format, args...))
	return b
}

func (b *CodeBuffer) NTformat(format string, args ...interface{}) *CodeBuffer {
	b.value.WriteString(NT)
	b.value.WriteString(fmt.Sprintf(format, args...))
	return b
}

func (b *CodeBuffer) NTTformat(format string, args ...interface{}) *CodeBuffer {
	b.value.WriteString(NTT)
	b.value.WriteString(fmt.Sprintf(format, args...))
	return b
}

func (b *CodeBuffer) NTTTformat(format string, args ...interface{}) *CodeBuffer {
	b.value.WriteString(NTTT)
	b.value.WriteString(fmt.Sprintf(format, args...))
	return b
}

func (b *CodeBuffer) NTTTTformat(format string, args ...interface{}) *CodeBuffer {
	b.value.WriteString(NTTTT)
	b.value.WriteString(fmt.Sprintf(format, args...))
	return b
}

func (b *CodeBuffer) NTTTTTformat(format string, args ...interface{}) *CodeBuffer {
	b.value.WriteString(NTTTTT)
	b.value.WriteString(fmt.Sprintf(format, args...))
	return b
}

func (b *CodeBuffer) NTTTTTTformat(format string, args ...interface{}) *CodeBuffer {
	b.value.WriteString(NTTTTTT)
	b.value.WriteString(fmt.Sprintf(format, args...))
	return b
}

func (b *CodeBuffer) NTTTTTTTformat(format string, args ...interface{}) *CodeBuffer {
	b.value.WriteString(NTTTTTTT)
	b.value.WriteString(fmt.Sprintf(format, args...))
	return b
}

func (b *CodeBuffer) NTTTTTTTTformat(format string, args ...interface{}) *CodeBuffer {
	b.value.WriteString(NTTTTTTTT)
	b.value.WriteString(fmt.Sprintf(format, args...))
	return b
}

func UpperFirst(str string) string {
	strRune := []rune(str)
	upperStr := strings.ToUpper(string(strRune[0:1])) + string(strRune[1:])
	return upperStr
}

// 下划线转成驼峰式
func UnderlineToCamel(str string) string {
	if strings.Contains(str, "_") {
		strPart := strings.Split(str, "_")
		var result string
		for i, v := range strPart {
			if i == 0 {
				result += v
			} else {
				result += UpperFirst(v)
			}
		}
		return result
	} else {
		return str
	}
}
